using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Collections.Generic;
using System;

namespace AuditionApp.Pages.QuestionBank
{
    public partial class MotiveTemplatePage : Page
    {
        public class ChildQuestion
        {
            public string Title { get; set; } = string.Empty;
            public List<string> Question { get; set; } = new List<string>();
            public List<string> Analysis { get; set; } = new List<string>();
            public List<string> Answer { get; set; } = new List<string>();
            public string? Source { get; set; }
            public string Content => string.Join("\n", Question);
        }

        private List<ChildQuestion> childQuestions = new List<ChildQuestion>();

        public MotiveTemplatePage()
        {
            InitializeComponent();
        }

        public void LoadQuestion(string title, string content, string analysis, List<ChildQuestion> children, string answer, string? source = null)
        {
            try
            {
                QuestionTitle.Text = title;
                QuestionContent.Text = content;
                LoadFormattedText(AnalyseContent, analysis);
                LoadFormattedText(AnswerContent, answer);

                childQuestions = children;
                foreach (var child in childQuestions)
                {
                    child.Source = source ?? string.Empty;
                }
                ChildrenQuestionList.ItemsSource = childQuestions;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载问题时出错: {ex.Message}\n{ex.StackTrace}");
            }
        }

        private void ChildQuestion_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (sender is Button button && button.CommandParameter is ChildQuestion question)
                {
                    var childPage = new ChildrenTemplatePage();
                    childPage.LoadQuestion(
                        question.Title,
                        question.Content,
                        string.Join("\n", question.Analysis ?? new List<string>()),
                        string.Join("\n", question.Answer ?? new List<string>()),
                        question.Source
                    );
                    NavigationService.Navigate(childPage);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载子问题时出错: {ex.Message}\n{ex.StackTrace}");
            }
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }

        private void LoadFormattedText(TextBlock textBlock, string text)
        {
            textBlock.Inlines.Clear();
            
            // 处理 """ 标记的文本块
            if (text.StartsWith("\"\"\"") && text.EndsWith("\"\"\""))
            {
                // 移除首尾的 """
                text = text.Substring(3, text.Length - 6);
            }
            
            // 使用 ** 来标记需要加粗的文本
            var parts = text.Split(new[] { "**" }, System.StringSplitOptions.None);
            for (int i = 0; i < parts.Length; i++)
            {
                if (i % 2 == 0)
                {
                    // 普通文本
                    textBlock.Inlines.Add(new Run(parts[i]));
                }
                else
                {
                    // 加粗文本
                    textBlock.Inlines.Add(new Run(parts[i]) { FontWeight = FontWeights.Bold });
                }
            }
        }
    }
} 
